package com.wlz.copy;

import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;

/**
 * @author： wlz
 * @Date 2021/2/24 9:42
 * @description： TODO 深拷贝
 * @version: 1.0
 */
public class DeepCopy implements Cloneable {

    String name;

    Child child;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        final DeepCopy clone = (DeepCopy) super.clone();
        clone.child = (Child) this.child.clone();
        return clone;
    }

    @SneakyThrows
    public static void main(String[] args) {
        final DeepCopy deepCopy = new DeepCopy();
        deepCopy.name = "qin";
        deepCopy.child = new Child();
        deepCopy.child.age = 18;

        // 拷⻉
        DeepCopy clone = (DeepCopy) deepCopy.clone();
        clone.name = "pear13";
        clone.child.age = 19;

        System.out.println(JSON.toJSONString(deepCopy));
        System.out.println(JSON.toJSONString(clone));
    }

}
